package com.zhuiYun.project.api.commodity.service.impl;
import com.sun.deploy.util.StringUtils;
import com.zhuiYun.project.api.commodity.entity.Commodity;
import com.zhuiYun.project.api.commodity.mapper.CommodityMapper;
import com.zhuiYun.project.api.commodity.model.CommodityModel;
import com.zhuiYun.project.api.commodity.service.CommodityService;

import com.zhuiYun.project.api.commodityType.entity.CommodityType;
import com.zhuiYun.project.api.commodityType.mapper.CommodityTypeMapper;
import com.zhuiYun.project.api.sysUser.entiy.SysUser;
import com.zhuiYun.project.common.errorCode.CommonException;
import com.zhuiYun.project.common.errorCode.EmErrorCode;
import com.zhuiYun.project.common.response.CommonConstants;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.*;
import com.zhuiYun.project.util.pageUtil.MybatisPageHelper;
import com.zhuiYun.project.util.pageUtil.PageRequest;
import com.zhuiYun.project.util.pageUtil.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CommodityServiceImpl
 * @Description TODO
 * Author 高
 * @Date 2023/4/3 10:30
 * @Version 1.0
 **/
@Service
public class CommodityServiceImpl implements CommodityService {
    @Autowired
    CommodityMapper commodityMapper;
    @Autowired
    CommodityTypeMapper commodityTypeMapper;
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    PrimaryKeyUtil primaryKeyUtil;
    @Autowired
    UploadFileUtils uploadFile;
    /*
    * @Author 高哲
    * @Description 分页查询
    * @Date 2023/4/3 14:11
    * @Return pageRequest
    * @Param commodityModel
    */
    @Override
    public CommonResult getCommodityByPage(CommodityModel commodityModel, PageRequest pageRequest) {
        // 设置分页
        MybatisPageHelper.startPage(pageRequest);
        Example example = new Example(Commodity.class);
        Example.Criteria commodityCriteria = example.createCriteria();
        // 设置模糊查询
        if(ObjectUtils.isNotEmpty(commodityModel.getCommodityName())){
            commodityCriteria.andLike("commodityName","%"+commodityModel.getCommodityName()+"%");
        }
        if(ObjectUtils.isNotEmpty(commodityModel.getTypeId())){
            commodityCriteria.andCondition("type_id in ("+commodityModel.getTypeId()+")");
        }
        //未被删除
        commodityCriteria.andEqualTo("delFlag", 0);
        // 添加倒序排列的代码
        example.orderBy("id").desc();
        PageResult pageResult = MybatisPageHelper.getPageResult(commodityMapper.selectByExample(example));
        return CommonResult.ok(pageResult);
    }
    /*
     * @Author 高哲
     * @Description 待上架分页查询
     * @Date 2023/4/18 08:11
     * @Return pageRequest
     * @Param commodityModel
     */
    @Override
    public CommonResult getCommodityByPage1(CommodityModel commodityModel, PageRequest pageRequest) {
        // 设置分页
        MybatisPageHelper.startPage(pageRequest);
        Example example = new Example(Commodity.class);
        Example.Criteria commodityCriteria = example.createCriteria();
        // 设置模糊查询
        if(ObjectUtils.isNotEmpty(commodityModel.getCommodityName())){
            commodityCriteria.andLike("commodityName","%"+commodityModel.getCommodityName()+"%");
        }
        //下架
        commodityCriteria.andEqualTo("delFlag", 1);
        // 添加倒序排列的代码
        example.orderBy("id").desc();
        PageResult pageResult = MybatisPageHelper.getPageResult(commodityMapper.selectByExample(example));
        return CommonResult.ok(pageResult);
    }
    /*
     * @Author 高哲
     * @Description 回收站分页查询
     * @Date 2023/4/18 09:38
     * @Return pageRequest
     * @Param commodityModel
     */
    @Override
    public CommonResult getCommodityByPage2(CommodityModel commodityModel, PageRequest pageRequest) {
        // 设置分页
        MybatisPageHelper.startPage(pageRequest);
        Example example = new Example(Commodity.class);
        Example.Criteria commodityCriteria = example.createCriteria();
        // 设置模糊查询
        if(ObjectUtils.isNotEmpty(commodityModel.getCommodityName())){
            commodityCriteria.andLike("commodityName","%"+commodityModel.getCommodityName()+"%");
        }
        //下架
        commodityCriteria.andEqualTo("delFlag", 2);
        // 添加倒序排列的代码
        example.orderBy("id").desc();
        PageResult pageResult = MybatisPageHelper.getPageResult(commodityMapper.selectByExample(example));
        return CommonResult.ok(pageResult);
    }

    /*
     * @Author 高哲
     * @Description 新增商品
     * @Date 2023/4/3 19:41
     * @Return
     * @Param commodityModel
     */
    @Override
    public int CommodityAdd(MultipartFile[] goodsImg, MultipartFile[] viewImg, CommodityModel publish){
        publish.setCreateTime(new Date());
        publish.setUpdateTime(new Date());
        publish.setDelFlag(0);
        int i=0;
        if(goodsImg!=null){
            List<String> ImageId = new ArrayList<>();
            for (int ids=0;ids <goodsImg.length;ids++) {
                String upl = uploadFile.uploadFile(goodsImg[ids]);
                if (ObjectUtils.isNotEmpty(upl)){
                    ImageId.add(upl);
                }else{
                    return 0;
                }
            }
            publish.setImageId(StringUtils.join(ImageId, ","));
        }
        if(viewImg!=null){
            List<String> ImageViewId = new ArrayList<>();
            for (int a=0;a <viewImg.length;a++) {
                String upl = uploadFile.uploadFile(viewImg[a]);
                if (ObjectUtils.isNotEmpty(upl)){
                    ImageViewId.add(upl);
                }else{
                    return 0;
                }
            }
            publish.setImageViewId(StringUtils.join(ImageViewId,","));
        }
        Commodity commodity = new Commodity();
        BeanUtils.copy(publish, commodity);
        i = commodityMapper.insertSelective(commodity);
        RedisUtil.set(CommonConstants.COMMODITY_ID + commodity.getId(),commodity);
        return i;
    }
    /*
     * @Author 高哲
     * @Description 新增未上架商品
     * @Date 2023/4/18 19:41
     * @Return
     * @Param commodityModel
     */
    @Override
    public int CommodityAdd1(MultipartFile[] goodsImg, MultipartFile[] viewImg, CommodityModel publish) throws CommonException {
        publish.setCreateTime(new Date());
        publish.setUpdateTime(new Date());
        publish.setDelFlag(1);
        int i=0;
        if(goodsImg!=null){
            List<String> ImageId = new ArrayList<>();
            for (int ids=0;ids <goodsImg.length;ids++) {
                String upl = uploadFile.uploadFile(goodsImg[ids]);
                if (ObjectUtils.isNotEmpty(upl)){
                    ImageId.add(upl);
                }else{
                    return 0;
                }
            }
            publish.setImageId(StringUtils.join(ImageId, ","));
        }
        if(viewImg!=null){
            List<String> ImageViewId = new ArrayList<>();
            for (int a=0;a <viewImg.length;a++) {
                String upl = uploadFile.uploadFile(viewImg[a]);
                if (ObjectUtils.isNotEmpty(upl)){
                    ImageViewId.add(upl);
                }else{
                    return 0;
                }
            }
            publish.setImageViewId(StringUtils.join(ImageViewId,","));
        }
        Commodity commodity = new Commodity();
        BeanUtils.copy(publish, commodity);
        i = commodityMapper.insertSelective(commodity);
        RedisUtil.set(CommonConstants.COMMODITY_ID + commodity.getId(),commodity);
        return i;
    }

    /**
     * @Author 高哲
     * @Description 商品修改
     * * @Date 2023/4/5 08:41
     * @param
     * @return
     */
    @Override
    public int CommodityUpdate(MultipartFile[] goodsImg, MultipartFile[] viewImg, CommodityModel publish) throws CommonException {
        publish.setUpdateTime(new Date());
        int i=0;
        if(goodsImg!=null){
            List<String> ImageId = new ArrayList<>();
            for (int ids=0;ids <goodsImg.length;ids++) {
                String upl = uploadFile.uploadFile(goodsImg[ids]);
                if (ObjectUtils.isNotEmpty(upl)){
                    ImageId.add(upl);
                }else{
                    return 0;
                }
            }

            publish.addimg(StringUtils.join(ImageId, ","));
        }
        if(viewImg!=null){
            List<String> ImageViewId = new ArrayList<>();
            for (int a=0;a <viewImg.length;a++) {
                String upl = uploadFile.uploadFile(viewImg[a]);
                if (ObjectUtils.isNotEmpty(upl)){
                    ImageViewId.add(upl);
                }else{
                    return 0;
                }
            }
            publish.addviewimg(StringUtils.join(ImageViewId,","));
        }
        Commodity commodity = new Commodity();
        BeanUtils.copy(publish, commodity);
        i = commodityMapper.updateByPrimaryKeySelective(commodity);
        RedisUtil.set(CommonConstants.COMMODITY_ID + commodity.getId(),commodity);
        return i;
    }

    /**
     * @Author 高哲
     * @Description 逻辑删除下架
     * * @Date 2023/4/4 19:41
     * @param id
     * @return
     */
    @Override
    public CommonResult delCommodityById(Integer id) throws CommonException {
        //创建clientUserEntity
        Commodity commodity = new Commodity();
        //添加条件
        commodity.setId(id);
        commodity.setDelFlag(1);
        // 判断是否id为空
        int result = commodityMapper.updateByPrimaryKeySelective(commodity);
        if (result == 1) {
            return CommonResult.ok("下架成功");
        } else {
            return CommonResult.error("下架失败");
        }
    }
    /**
     * @Author 高哲
     * @Description 逻辑上架
     * * @Date 2023/4/18 09:25
     * @param id
     * @return
     */
    @Override
    public CommonResult delCommodityById1(Integer id) throws CommonException {
        //创建clientUserEntity
        Commodity commodity = new Commodity();
        //添加条件
        commodity.setId(id);
        commodity.setDelFlag(0);
        // 判断是否id为空
        int result = commodityMapper.updateByPrimaryKeySelective(commodity);
        if (result == 0) {
            return CommonResult.ok("上架成功");
        } else {
            return CommonResult.error("上架失败");
        }
    }
    /**
     * @Author 高哲
     * @Description 删除到回收站
     * * @Date 2023/4/18 09:35
     * @param id
     * @return
     */
    @Override
    public CommonResult delCommodityById2(Integer id) throws CommonException {
        //创建clientUserEntity
        Commodity commodity = new Commodity();
        //添加条件
        commodity.setId(id);
        commodity.setDelFlag(2);
        // 判断是否id为空
        int result = commodityMapper.updateByPrimaryKeySelective(commodity);
        if (result == 2) {
            return CommonResult.ok("删除成功");
        } else {
            return CommonResult.error("删除失败");
        }
    }
    /**
     * @Author 高哲
     * @Description 回收商品到未上架
     * * @Date 2023/4/18 09:42
     * @param id
     * @return
     */
    @Override
    public CommonResult delCommodityById3(Integer id) throws CommonException {
        //创建clientUserEntity
        Commodity commodity = new Commodity();
        //添加条件
        commodity.setId(id);
        commodity.setDelFlag(1);
        // 判断是否id为空
        int result = commodityMapper.updateByPrimaryKeySelective(commodity);
        if (result == 1) {
            return CommonResult.ok("回收成功");
        } else {
            return CommonResult.error("回收失败");
        }
    }

    /**
     * @Author 高哲
     * @Description 添加下拉框
     * * @Date 2023/4/10 11:21
     * @param
     * @return
     */
    @Override
    public List<CommodityType> getCommodityType() {
        List<CommodityType> getCommodityType = commodityTypeMapper.getCommodityType();
        return getCommodityType;
    }
    /**
     * @Author 高哲
     * @Description 删除回收站商品
     * * @Date 2023/4/10 11:21
     * @param
     * @return
     */
    @Override
    public CommonResult delCommodityById4(Integer id) throws CommonException {
        // 判断是否id为空
        if(ObjectUtils.isNotEmpty(id)){
            int i = commodityMapper.deleteByPrimaryKey(id);
            // 判断是否操作成功
            if(i>0){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.DELETE_ERROR);
            }
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }

    @Override
    public CommonResult commodityllist(CommodityModel commodityModel) {
        Example example = new Example(Commodity.class);
        List<Commodity> commodities = commodityMapper.commlistdiy();
        return CommonResult.ok(commodities);
    }
}
